{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "dc3acc16",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>store</th>\n",
       "      <th>product_group</th>\n",
       "      <th>product_code</th>\n",
       "      <th>stock_qty</th>\n",
       "      <th>cost</th>\n",
       "      <th>price</th>\n",
       "      <th>last_week_sales</th>\n",
       "      <th>last_month_sales</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Violet</td>\n",
       "      <td>PG2</td>\n",
       "      <td>4187</td>\n",
       "      <td>498</td>\n",
       "      <td>420.76</td>\n",
       "      <td>569.91</td>\n",
       "      <td>13</td>\n",
       "      <td>58</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Rose</td>\n",
       "      <td>PG2</td>\n",
       "      <td>4195</td>\n",
       "      <td>473</td>\n",
       "      <td>545.64</td>\n",
       "      <td>712.41</td>\n",
       "      <td>16</td>\n",
       "      <td>58</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Violet</td>\n",
       "      <td>PG2</td>\n",
       "      <td>4204</td>\n",
       "      <td>968</td>\n",
       "      <td>640.42</td>\n",
       "      <td>854.91</td>\n",
       "      <td>22</td>\n",
       "      <td>88</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Daisy</td>\n",
       "      <td>PG2</td>\n",
       "      <td>4219</td>\n",
       "      <td>241</td>\n",
       "      <td>869.69</td>\n",
       "      <td>1034.55</td>\n",
       "      <td>14</td>\n",
       "      <td>45</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Daisy</td>\n",
       "      <td>PG2</td>\n",
       "      <td>4718</td>\n",
       "      <td>1401</td>\n",
       "      <td>12.54</td>\n",
       "      <td>26.59</td>\n",
       "      <td>50</td>\n",
       "      <td>285</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>DaZhong</td>\n",
       "      <td>PG1</td>\n",
       "      <td>4387</td>\n",
       "      <td>418</td>\n",
       "      <td>420.76</td>\n",
       "      <td>569.91</td>\n",
       "      <td>13</td>\n",
       "      <td>58</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>Rose</td>\n",
       "      <td>PG1</td>\n",
       "      <td>4295</td>\n",
       "      <td>473</td>\n",
       "      <td>506.64</td>\n",
       "      <td>712.40</td>\n",
       "      <td>16</td>\n",
       "      <td>58</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>Violet</td>\n",
       "      <td>PG1</td>\n",
       "      <td>4504</td>\n",
       "      <td>968</td>\n",
       "      <td>580.42</td>\n",
       "      <td>854.61</td>\n",
       "      <td>62</td>\n",
       "      <td>88</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>Daisy</td>\n",
       "      <td>PG1</td>\n",
       "      <td>4219</td>\n",
       "      <td>241</td>\n",
       "      <td>679.69</td>\n",
       "      <td>1034.55</td>\n",
       "      <td>34</td>\n",
       "      <td>45</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>Daisy</td>\n",
       "      <td>PG1</td>\n",
       "      <td>4718</td>\n",
       "      <td>1201</td>\n",
       "      <td>15.54</td>\n",
       "      <td>26.59</td>\n",
       "      <td>60</td>\n",
       "      <td>285</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     store product_group  product_code  stock_qty    cost    price  \\\n",
       "0   Violet           PG2          4187        498  420.76   569.91   \n",
       "1     Rose           PG2          4195        473  545.64   712.41   \n",
       "2   Violet           PG2          4204        968  640.42   854.91   \n",
       "3    Daisy           PG2          4219        241  869.69  1034.55   \n",
       "4    Daisy           PG2          4718       1401   12.54    26.59   \n",
       "5  DaZhong           PG1          4387        418  420.76   569.91   \n",
       "6     Rose           PG1          4295        473  506.64   712.40   \n",
       "7   Violet           PG1          4504        968  580.42   854.61   \n",
       "8    Daisy           PG1          4219        241  679.69  1034.55   \n",
       "9    Daisy           PG1          4718       1201   15.54    26.59   \n",
       "\n",
       "   last_week_sales  last_month_sales  \n",
       "0               13                58  \n",
       "1               16                58  \n",
       "2               22                88  \n",
       "3               14                45  \n",
       "4               50               285  \n",
       "5               13                58  \n",
       "6               16                58  \n",
       "7               62                88  \n",
       "8               34                45  \n",
       "9               60               285  "
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#读取数据源  sales_data.csv\n",
    "import pandas as pd\n",
    "df = pd.read_csv('./data/sales_data.csv')\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "81fbc837",
   "metadata": {},
   "source": [
    "店铺，产品类型，编码，商品库存量，成本价，销售价，上周销售量，上月销售量"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4247eec2",
   "metadata": {},
   "source": [
    "1.计算每个店铺的平均库存量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "269bf6df",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "store\n",
       "DaZhong    418.000000\n",
       "Daisy      771.000000\n",
       "Rose       473.000000\n",
       "Violet     811.333333\n",
       "Name: stock_qty, dtype: float64"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.groupby('store')['stock_qty'].mean()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "53932a10",
   "metadata": {},
   "source": [
    "2.计算每个店铺的平均库存量以及产品的平均销售价格"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "d15d89f4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>stock_qty</th>\n",
       "      <th>price</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>store</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>DaZhong</th>\n",
       "      <td>418.000000</td>\n",
       "      <td>569.910</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Daisy</th>\n",
       "      <td>771.000000</td>\n",
       "      <td>530.570</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Rose</th>\n",
       "      <td>473.000000</td>\n",
       "      <td>712.405</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Violet</th>\n",
       "      <td>811.333333</td>\n",
       "      <td>759.810</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          stock_qty    price\n",
       "store                       \n",
       "DaZhong  418.000000  569.910\n",
       "Daisy    771.000000  530.570\n",
       "Rose     473.000000  712.405\n",
       "Violet   811.333333  759.810"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.groupby('store')[['stock_qty','price']].mean()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3e057606",
   "metadata": {},
   "source": [
    "3.计算每个店铺的平均库存量和最大的库存数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "6f8eac0e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>avg_stock_qty</th>\n",
       "      <th>max_stock_qty</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>store</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>DaZhong</th>\n",
       "      <td>418.000000</td>\n",
       "      <td>418</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Daisy</th>\n",
       "      <td>771.000000</td>\n",
       "      <td>1401</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Rose</th>\n",
       "      <td>473.000000</td>\n",
       "      <td>473</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Violet</th>\n",
       "      <td>811.333333</td>\n",
       "      <td>968</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         avg_stock_qty  max_stock_qty\n",
       "store                                \n",
       "DaZhong     418.000000            418\n",
       "Daisy       771.000000           1401\n",
       "Rose        473.000000            473\n",
       "Violet      811.333333            968"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "avg_max_qty = df.groupby('store').agg(avg_stock_qty=('stock_qty','mean'),\n",
    "                                       max_stock_qty=('stock_qty',max))\n",
    "avg_max_qty"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "504c6c02",
   "metadata": {},
   "source": [
    "4.对聚合结果重命名（对3中的结果重命名）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "65be58f1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>平均库存量</th>\n",
       "      <th>最大库存数据</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>store</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>DaZhong</th>\n",
       "      <td>418.000000</td>\n",
       "      <td>418</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Daisy</th>\n",
       "      <td>771.000000</td>\n",
       "      <td>1401</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Rose</th>\n",
       "      <td>473.000000</td>\n",
       "      <td>473</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Violet</th>\n",
       "      <td>811.333333</td>\n",
       "      <td>968</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              平均库存量  最大库存数据\n",
       "store                      \n",
       "DaZhong  418.000000     418\n",
       "Daisy    771.000000    1401\n",
       "Rose     473.000000     473\n",
       "Violet   811.333333     968"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "columns_rename = avg_max_qty.rename(columns={'avg_stock_qty':'平均库存量',\n",
    "                             'max_stock_qty':'最大库存数据'})\n",
    "columns_rename"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ac5ae2a7",
   "metadata": {},
   "source": [
    "5.计算每个店铺的平均库存量、最大库存量、产品销售的均价以及产品上周销售最大值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "4267d17e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr:last-of-type th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th colspan=\"2\" halign=\"left\">stock_qty</th>\n",
       "      <th>price</th>\n",
       "      <th>last_week_sales</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th>mean</th>\n",
       "      <th>max</th>\n",
       "      <th>mean</th>\n",
       "      <th>max</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>store</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>DaZhong</th>\n",
       "      <td>418.000000</td>\n",
       "      <td>418</td>\n",
       "      <td>569.910</td>\n",
       "      <td>13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Daisy</th>\n",
       "      <td>771.000000</td>\n",
       "      <td>1401</td>\n",
       "      <td>530.570</td>\n",
       "      <td>60</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Rose</th>\n",
       "      <td>473.000000</td>\n",
       "      <td>473</td>\n",
       "      <td>712.405</td>\n",
       "      <td>16</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Violet</th>\n",
       "      <td>811.333333</td>\n",
       "      <td>968</td>\n",
       "      <td>759.810</td>\n",
       "      <td>62</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          stock_qty          price last_week_sales\n",
       "               mean   max     mean             max\n",
       "store                                             \n",
       "DaZhong  418.000000   418  569.910              13\n",
       "Daisy    771.000000  1401  530.570              60\n",
       "Rose     473.000000   473  712.405              16\n",
       "Violet   811.333333   968  759.810              62"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data5 = df.groupby('store').agg({'stock_qty':['mean','max'],\n",
    "                                 'price':'mean',\n",
    "                                 'last_week_sales':'max'})\n",
    "data5"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e9b08c3b",
   "metadata": {},
   "source": [
    "6.计算每个店铺的平均库存数量、产品销售均价"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "d36ad966",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>stock_qty</th>\n",
       "      <th>price</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>store</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>DaZhong</th>\n",
       "      <td>418.000000</td>\n",
       "      <td>569.91</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Daisy</th>\n",
       "      <td>771.000000</td>\n",
       "      <td>1034.55</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Rose</th>\n",
       "      <td>473.000000</td>\n",
       "      <td>712.41</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Violet</th>\n",
       "      <td>811.333333</td>\n",
       "      <td>854.91</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          stock_qty    price\n",
       "store                       \n",
       "DaZhong  418.000000   569.91\n",
       "Daisy    771.000000  1034.55\n",
       "Rose     473.000000   712.41\n",
       "Violet   811.333333   854.91"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "avg_qty_price = df.groupby('store').agg({'stock_qty':'mean',\n",
    "                                         'price':'max'})\n",
    "avg_qty_price"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "387e610b",
   "metadata": {},
   "source": [
    "7.计算每个店铺不同产品类型的上周销量的均值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "edb1c235",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "store    product_group\n",
       "DaZhong  PG1              13.0\n",
       "Daisy    PG1              47.0\n",
       "         PG2              32.0\n",
       "Rose     PG1              16.0\n",
       "         PG2              16.0\n",
       "Violet   PG1              62.0\n",
       "         PG2              17.5\n",
       "Name: last_week_sales, dtype: float64"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "avg_last_week_sales = df.groupby(['store','product_group'])['last_week_sales'].mean()\n",
    "avg_last_week_sales"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dd82f69d",
   "metadata": {},
   "source": [
    "8.计算每个店铺不同产品类型的上周销量的均值,而后销量的均值进行降序排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "d5b9c1d0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>avg_sales</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>store</th>\n",
       "      <th>product_group</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Violet</th>\n",
       "      <th>PG1</th>\n",
       "      <td>62.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">Daisy</th>\n",
       "      <th>PG1</th>\n",
       "      <td>47.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>PG2</th>\n",
       "      <td>32.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Violet</th>\n",
       "      <th>PG2</th>\n",
       "      <td>17.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">Rose</th>\n",
       "      <th>PG1</th>\n",
       "      <td>16.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>PG2</th>\n",
       "      <td>16.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>DaZhong</th>\n",
       "      <th>PG1</th>\n",
       "      <td>13.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                       avg_sales\n",
       "store   product_group           \n",
       "Violet  PG1                 62.0\n",
       "Daisy   PG1                 47.0\n",
       "        PG2                 32.0\n",
       "Violet  PG2                 17.5\n",
       "Rose    PG1                 16.0\n",
       "        PG2                 16.0\n",
       "DaZhong PG1                 13.0"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "avg_sales = df.groupby(['store','product_group']).agg(avg_sales=\n",
    "                                                      ('last_week_sales','mean'))\n",
    "avg_sales_desc = avg_sales.sort_values(by='avg_sales',ascending=False)\n",
    "avg_sales_desc"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "95f3af1a",
   "metadata": {},
   "source": [
    "9.统计每个店铺的上周销售量最大值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "c379e372",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "store\n",
       "DaZhong    13\n",
       "Daisy      60\n",
       "Rose       16\n",
       "Violet     62\n",
       "Name: last_week_sales, dtype: int64"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "max_sales = df.groupby('store')['last_week_sales'].max()\n",
    "max_sales"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "24f7f7c8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "store     \n",
       "DaZhong  5    13\n",
       "Daisy    9    60\n",
       "Rose     1    16\n",
       "Violet   7    62\n",
       "Name: last_week_sales, dtype: int64"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#nlargest从数据帧或序列中获取n个最大值\n",
    "max_sales_nl = df.groupby('store')['last_week_sales'].nlargest(1)\n",
    "max_sales_nl"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3ba5270c",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
